/**
 * Created by lamb
 * Time: 11-12-5 下午1:35
 */
var http = require('http');
var channels = new Object();
channels.photo = '/channels/first/post/28.html';//photo
channels.model = '/channels/first/post/23.html';//model
channels.design = '/channels/first/post/41.html';//design
channels.actor = '/channels/first/post/143.html';//actor
channels.music = '/channels/first/post/13.html'; //music
channels.movie = '/channels/first/post/1.html'; // movie
channels.media = '/channels/first/post/53.html'; //media
channels.art = '/channels/first/post/71.html';//art

function get_channel_logout(channel, callback) {
    var items = new Array();

    var options = {
        host: 'www.moko.cc',
        port: 80,
        path: channels[channel],
        method: 'GET'
    };

    var req = http.request(options, function(res) {
        //console.log('STATUS: ' + res.statusCode);
        //console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        var buffers = [], size = 0;
        res.on('data', function (buffer) {
            buffers.push(buffer);
            size += buffer.length;
        });
        res.on('end', function() {
            var html = buffers.toString();
            var mvp_show_start = html.lastIndexOf('<div class="sBox bd cf">');
            var mvp_show_end = html.indexOf('<div class="content uPush bd cf" id="hangyeStarDiv">');
            html = html.substring(mvp_show_start, mvp_show_end);
            var show_small_start = html.indexOf('<div class="show small">');
            var show_small_end = html.lastIndexOf('</div>');
            html = html.substring(show_small_start, show_small_end);
            while (html.indexOf(",") != -1) {
                html = html.replace(",", "");
            }
            //console.log(html);

            var separate = '<div class="show small">';
            var href_separate = 'href="';
            var quotation_mark = '"';
            var src2_separate = 'src2="';
            var name_separate = 'target="_blank">';
            var auth_separate = '认证会员';
            var angle_brackets_left = '<';
            var angle_brackets_right = '>';
            var span_separate = '<span';

            while (html.indexOf(separate) != -1) {
                var item = new Object();

                var item_start = html.lastIndexOf(separate);
                var item_html = html.substring(item_start);
                html = html.substring(0, item_start);

                var href_start = item_html.indexOf(href_separate) + href_separate.length;
                var href_end = item_html.indexOf(quotation_mark, href_start);
                item.href = item_html.substring(href_start, href_end);

                var src2_start = item_html.indexOf(src2_separate) + src2_separate.length;
                var src2_end = item_html.indexOf(quotation_mark, src2_start);
                item.src2 = item_html.substring(src2_start, src2_end);
                //截取一下target在前面也有重复，前面没用的都去掉
                item_html = item_html.substring(src2_end);

                var name_start = item_html.indexOf(name_separate) + name_separate.length;
                var name_end = item_html.indexOf(angle_brackets_left, name_start);
                item.name = item_html.substring(name_start, name_end);
                //是否是认证会员
                var is_auth = false;
                if (item_html.indexOf(auth_separate) != -1)is_auth = true;
                item.auth = is_auth;

                //先找span,从这里往后寻找>就是开始(+1不包含>)，下一个<就是结束；
                var span_start = item_html.indexOf(span_separate);
                var rank_start = item_html.indexOf(angle_brackets_right, span_start) + 1;
                var rank_end = item_html.indexOf(angle_brackets_left, rank_start);
                item.rank = item_html.substring(rank_start, rank_end);

                //就是rank_end开始找,也就是从</span>开始找第一个>(+1不包含>),到第一个<结束
                var duty_start = item_html.indexOf(angle_brackets_right, rank_end) + 1;
                var duty_end = item_html.indexOf(angle_brackets_left, duty_start);
                item.duty = item_html.substring(duty_start, duty_end);
                items.unshift(item);
            }

            //console.log(items);
            //console.log(items.length);
            callback(items);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    // write data to request body
    //req.write('data\n');
    //req.write('data\n');
    req.end();
}
exports.get_channel_logout = get_channel_logout;